package com.xiguthings.xiniu.iot.post.controller;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiguthings.xiniu.iot.common.PostData;
import com.xiguthings.xiniu.iot.post.common.entity.PostResult;
import com.xiguthings.xiniu.iot.post.kafka.KafkaClient;
import com.xiguthings.xiniu.iot.post.poster.PostCenter;
import com.xiguthings.xiniu.iot.post.recorder.Recorder;

@Component
public class DataPostContrller {
	private Logger LOGGER = org.slf4j.LoggerFactory.getLogger(DataPostContrller.class);

    private Recorder recorder;// TODO 暂时不实现

    @Autowired
    private PostCenter postCenter;

    @Autowired
    private KafkaClient kafkaClient;

    /**
     * 接收从kafka过来的消息
     *
     * @param data
     */
    public void receiveData(PostData data) {
        if (data == null) {
            return;
        }
        
		  postCenter.postData(data);
    }

    /**
     * 再次尝试发送数据
     *
     * @param data
     */
    public void tryPostAgain(PostData data) {
        kafkaClient.produceData(data);
    }

    /**
     * 记录发送的结果
     *
     * @param postResult
     */
    public void recordResultPost(PostResult postResult) {
        if (postResult != null || recorder == null) {
            return;
        }
        recorder.recordResultPost(postResult);
    }
}
